package com.gx.controller;


import com.github.xiaoymin.knife4j.annotations.ApiSort;
import com.gx.common.result.AppResult;
import com.gx.model.dto.task.StartFlowTaskInfoDto;
import com.gx.service.FlowInstanceService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Author Liu-pj
 * @Date 2022/9/14 12:15
 * @Desc 工作流流程实例管理
 **/
@ApiSort(22)
@Slf4j
@Api(tags = "实例管理")
@RestController
@RequestMapping("/flowable/instance")
public class FlowInstanceController {

    @Autowired
    private FlowInstanceService flowInstanceService;


    @ApiOperation("启动流程实例[触发流程]")
    @PostMapping(value = "/startProcessInstance")
    public AppResult<String> startProcessInstance(@ApiParam(value = "流程实例ID", required = true) @RequestBody StartFlowTaskInfoDto dto) {
        flowInstanceService.startProcessInstanceById(dto);
        return AppResult.success();
    }

    @ApiOperation("强制结束流程实例")
    @PostMapping(value = "/stopProcessInstance")
    public AppResult<String> stopProcessInstance(@ApiParam(value = "流程实例ID", required = true) @RequestParam String instanceId) {
        flowInstanceService.stopProcessInstance(instanceId);
        return AppResult.success();
    }

    @ApiOperation(value = "删除流程实例[删除流程]", notes = "流程定义还在,其他信息级联删除")
    @GetMapping(value = "/delete")
    public AppResult<String> delete(@ApiParam(value = "流程实例ID", required = true) @RequestParam String instanceId) {
        flowInstanceService.delete(instanceId, "实例删除");
        return AppResult.success();
    }

    /**
     * 流程实例挂起暂停
     * @param instanceId 实例id
     */
    @ApiOperation(value = "激活或挂起流程实例")
    @GetMapping(value = "/updateState")
    public AppResult<String> updateState(@ApiParam(value = "流程实例ID", required = true) @RequestParam String instanceId) {
        flowInstanceService.updateState( instanceId);
        return AppResult.success();
    }
}